// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   NseerId.java

package include.nseer_cookie;

import include.nseer_db.nseer_db;
import include.tree_index.Nseer;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.Date;

public class NseerId
{

	public NseerId()
	{
	}

	public static String getId(String path, String dbase)
	{
		String file_path = "";
		String mod = "";
		String id = "";
		if (path.indexOf("/classes/") != -1)
		{
			file_path = path.split("/classes/")[1].substring(0, path.split("/classes/")[1].length() - 1);
			mod = file_path.split("/")[0];
		} else
		{
			file_path = path;
			mod = file_path.split("/")[0];
		}
		try
		{
			nseer_db erp_db = new nseer_db(dbase);
			Nseer n = new Nseer();
			mod = mod.equals("document") ? "erpPlatform" : mod;
			file_path = file_path.equals("document/techFile") ? "erpPlatform/techFile" : file_path;
			file_path = file_path.equals("document/supportFile") ? "erpPlatform/supportFile" : file_path;
			String sql = (new StringBuilder("select file_id from ")).append(mod).append("_tree where file_path='").append(file_path).append("'").toString();
			ResultSet rs = erp_db.executeQuery(sql);
			if (rs.next())
			{
				id = rs.getString("file_id");
				id = (new StringBuilder(String.valueOf(id))).append("000000").toString();
				id = id.substring(0, 6);
				int filenum_temp = readTime(dbase, file_path);
				writeTime(dbase, file_path);
				String time = "";
				Date now = new Date();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
				time = formatter.format(now);
				id = (new StringBuilder(String.valueOf(id))).append(time).append(filenum_temp).toString();
			}
			erp_db.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return id;
	}

	public static int readTime(String dbase, String file_path)
	{
		int filenum = 0;
		Date now = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String time = formatter.format(now);
		nseer_db erp_db = new nseer_db(dbase);
		String kind_name = file_path.replaceAll("/", "_");
		String kind = (new StringBuilder(String.valueOf(kind_name))).append("_").append(time).toString();
		try
		{
			String sql = (new StringBuilder("select * from security_counter where kind_name='")).append(kind_name).append("'").toString();
			ResultSet rs = erp_db.executeQuery(sql);
			if (rs.next())
			{
				if (kind.equals(rs.getString("kind")))
				{
					filenum = Integer.parseInt(rs.getString("count_value"));
				} else
				{
					String sqla = (new StringBuilder("update security_counter set kind='")).append(kind).append("', count_value='100001' where kind_name='").append(kind_name).append("'").toString();
					erp_db.executeUpdate(sqla);
					filenum = 0x186a1;
				}
			} else
			{
				String sqla = (new StringBuilder("insert into security_counter(kind,count_value,kind_name) values ('")).append(kind).append("','100001','").append(kind_name).append("')").toString();
				erp_db.executeUpdate(sqla);
				filenum = 0x186a1;
			}
			erp_db.close();
		}
		catch (Exception exception) { }
		return filenum;
	}

	public static void writeTime(String dbase, String file_path)
	{
		int filenum = 0;
		Date now = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String time = formatter.format(now);
		nseer_db erp_db = new nseer_db(dbase);
		String kind_name = file_path.replaceAll("/", "_");
		String kind = (new StringBuilder(String.valueOf(kind_name))).append("_").append(time).toString();
		try
		{
			String sql = (new StringBuilder("select * from security_counter where kind_name='")).append(kind_name).append("'").toString();
			ResultSet rs = erp_db.executeQuery(sql);
			if (rs.next())
			{
				filenum = Integer.parseInt(rs.getString("count_value"));
				filenum++;
				String sqla = (new StringBuilder("update security_counter set count_value='")).append(filenum).append("' where kind_name='").append(kind_name).append("'").toString();
				erp_db.executeUpdate(sqla);
			}
			erp_db.close();
		}
		catch (Exception exception) { }
	}
}
